'''
Created on May 21, 2012

@author: tel
'''
import os
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
from pylab import setp
from plothelper import Unpickle, Repickle
from scipy import interpolate

def OffTicks(ax, list):
    for entry in list:
        if entry=='x':
            setp(ax.get_xticklabels(), visible=False)
        elif entry=='y':
            setp(ax.get_yticklabels(), visible=False)

def Bezier(eps, wt_rmsd, mut_rmsd, repickle=True, outdir='home/tel/Dropbox/remote', outfile='Bezier'):
    xlabel = r'$\epsilon_{prot}$'
    ylabel = 'RMSD'

    if repickle:
        Repickle(outdir, outfile, eps, wt_rmsd, mut_rmsd)
    
    fig = plt.figure(1)
    axes = plt.axes()
    
    axes.set_xlabel(xlabel)
    axes.set_ylabel(ylabel)

    for tick in axes.xaxis.get_major_ticks():
        tick.label.set_fontsize('small')
        tick.label.set_rotation(45)
    for tick in axes.yaxis.get_major_ticks():
        tick.label.set_fontsize('small')
    
    if outfile=='rmsd_bezier':
        axes.scatter(eps, wt_rmsd, color='b')     
        axes.scatter(eps, mut_rmsd, color='g')
        axes.set_xlim(left=2,right=20)
        axes.set_ylim(bottom=0, top=8)
        
        axes.axhline(y=1.36, color='b', linestyle='--') #sh null model line
        axes.axhline(y=2.3, color='g', linestyle='--')  #3bdc mutants null model line
        
        eps_smooth = np.arange(min(eps),max(eps),float(max(eps))/200)
        wt_tck = interpolate.splrep(eps,wt_rmsd,s=0)
        wt_rmsd_smooth = interpolate.splev(eps_smooth,wt_tck,der=0)
        mut_tck = interpolate.splrep(eps,mut_rmsd,s=0)
        mut_rmsd_smooth = interpolate.splev(eps_smooth,mut_tck,der=0)
        axes.plot(eps_smooth,wt_rmsd_smooth, color='b')
        axes.plot(eps_smooth,mut_rmsd_smooth, color='g')
    elif outfile=='rmsd_indiv_bezier':
        for res_rmsd in wt_rmsd:
            if len(eps)!=len(res_rmsd):
                continue
            axes.scatter(eps, res_rmsd, color='b')
            axes.set_xlim(left=7.5,right=12.5)
            axes.set_ylim(bottom=0, top=2)
            
            eps_smooth = np.arange(min(eps),max(eps),float(max(eps))/200)
            res_tck = interpolate.splrep(eps,res_rmsd,s=0)
            res_rmsd_smooth = interpolate.splev(eps_smooth,res_tck,der=0)
            axes.plot(eps_smooth,res_rmsd_smooth, color='b')
        
    fig.savefig(os.path.join(outdir, outfile+'.png'), bbox_inches='tight', transparent=True)
    fig.clf()
    
if __name__=="__main__":
    Unpickle(indir='/home/tel/benchmark/dunbrack', infile='rmsd_bezier', outdir='/home/tel/benchmark/dunbrack', outfile='rmsd_bezier', argn=3, plotter=Bezier)
    Unpickle(indir='/home/tel/benchmark/dunbrack', infile='rmsd_indiv_bezier', outdir='/home/tel/benchmark/dunbrack', outfile='rmsd_indiv_bezier', argn=3, plotter=Bezier)

#    fig = plt.figure(1)
#    axes = plt.axes()
#
#    axes.set_xlabel('Minimum distance\nto active site $(\AA)$', size = 40, multialignment = 'center')
#    axes.set_ylabel(ylabel, size = 40, multialignment = 'center')
#    fig.set_size_inches(11,8)
#    axes.set_xticklabels(['','10','20','30','40','50'])
#    xtick = axes.get_xticklabels()
#    for tick in xtick:
#        tick.set_fontsize(30)
#    #axes.set_yticklabels([])
#    ytick = axes.get_yticklabels()
#    for tick in ytick:
#        tick.set_fontsize(30)
#    axes.hist(x_high_c, bins=30, color='b')
#    axes.set_xlim(0, 50)
#    fig.savefig(fname+'_highc.png', bbox_inches='tight', transparent=True)
#    axes.cla()
#    
#    axes.set_xlabel('Minimum distance\nto active site $(\AA)$', size = 40, multialignment = 'center')
#    axes.set_ylabel(ylabel, size = 40, multialignment = 'center')
#    fig.set_size_inches(11,8)
#    #axes.set_xticklabels([])
#    #axes.set_yticklabels([])
#    axes.set_xticklabels(['','10','20','30','40','50'])
#    xtick = axes.get_xticklabels()
#    for tick in xtick:
#        tick.set_fontsize(30)
#    axes.set_yticklabels(['0','','1','','2'])
#    ytick = axes.get_yticklabels()
#    for tick in ytick:
#        tick.set_fontsize(30)
#    
#    axes.hist(x_low_c, bins=30, color='r')
#    axes.set_xlim(0, 50)
#    fig.savefig(fname+'_lowc.png', bbox_inches='tight', transparent=True)
#    fig.clf()
